package com.scm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.scm.dao.PoitemMapper;
import com.scm.dao.PomainMapper;
import com.scm.dao.ProductMapper;
import com.scm.entity.Poitem;
import com.scm.entity.Pomain;
import com.scm.exception.TransactionException;
import com.scm.service.IPomainService;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PomainServiceImpl implements IPomainService {

    @Autowired
    private PomainMapper pomainMapper;
    @Autowired
    private PoitemMapper poitemMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private HttpServletRequest req;

    @Override
    @Transactional
    public void insert(Pomain pomain) {
        this.pomainMapper.insert(pomain);
        HashMap<String, Object> itemMap = new HashMap();
        itemMap.put("poId", pomain.getPoId());
        itemMap.put("poitems", pomain.getPoitems());
        int n = this.poitemMapper.insert(itemMap);
        if (n <= 0) {
            throw new TransactionException("采购单明细新增失败");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("poitems", pomain.getPoitems());
        map.put("type", Integer.valueOf(1));
        n = this.productMapper.updatePoNum(map);
        if (n <= 0) {
            throw new TransactionException("修改采购失败");
        }
    }

    @Transactional
    @Override
    public void update(Pomain pomain) {
        int n = this.pomainMapper.update(pomain);
        if (n != 1) {
            throw new TransactionException("修改采购单主单失败");
        }
        List<Poitem> items = this.poitemMapper.selectByPoId(pomain.getPoId().longValue());
        if (items == null) {
            throw new TransactionException("没有查到采购单" + pomain.getPoId() + "����");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("poitems", items);
        map.put("type", Integer.valueOf(2));
        n = this.productMapper.updatePoNum(map);
        if (n <= 0) {
            throw new TransactionException("修改产品在途数失败");
        }
        n = this.poitemMapper.deleteByPoid(pomain.getPoId().longValue());
        if (n == 0) {
            throw new TransactionException("删除采购明细单失败");
        }
        HashMap<String, Object> itemMap = new HashMap();
        itemMap.put("poId", pomain.getPoId());
        itemMap.put("poitems", pomain.getPoitems());
        n = this.poitemMapper.insert(itemMap);
        if (n == 0) {
            throw new TransactionException("修改采购明细单失败");
        }
        itemMap.put("type", Integer.valueOf(1));
        n = this.productMapper.updatePoNum(itemMap);
        if (n <= 0) {
            throw new TransactionException("增加产品在途数失败");
        }
    }

    @Transactional
    @Override
    public void delete(Long poid) {
        List<Poitem> items = this.poitemMapper.selectByPoId(poid.longValue());
        if ((items == null) || (items.size() == 0)) {
            throw new TransactionException("没有查到采购单" + poid + "详情，删除失败");
        }
        HashMap<String, Object> map = new HashMap();
        map.put("poitems", items);
        map.put("type", Integer.valueOf(2));
        int n = this.productMapper.updatePoNum(map);
        if (n <= 0) {
            throw new TransactionException("修改产品在途数失败");
        }
        n = this.poitemMapper.deleteByPoid(poid.longValue());
        if (n == 0) {
            throw new TransactionException("删除采购单明细失败");
        }
        n = this.pomainMapper.deleteByPoId(poid);
        if (n != 1) {
            throw new TransactionException("删除采购单主信息失败");
        }
    }

    @Override
    public PageInfo<Pomain> selectByStatus(Integer page, Integer type, Integer payType) {
        if (type == null) {
            throw new TransactionException("入参不能为空");
        }
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        m.put("type", type);
        m.put("payType", payType);
        if ((type.intValue() == 1) || (type.intValue() == 4)) {
            String account = (String) this.req.getAttribute("login");
            m.put("account", account);
        }
        List<Pomain> pomains = this.pomainMapper.selectPomain(m);
        System.out.println(pomains);
        PageInfo<Pomain> info = new PageInfo(pomains);
        return info;
    }

    @Override
    public PageInfo<Pomain> selectReport(Integer page, String time) {
        PageHelper.startPage(page == null ? 1 : page.intValue(), 10);
        HashMap<String, Object> m = new HashMap();
        m.put("startDate", time + "-01");
        m.put("endDate", time + "-31");
        List<Pomain> pomains = this.pomainMapper.selectCondition(m);
        PageInfo<Pomain> info = new PageInfo(pomains);
        return info;
    }
}
